package club.smartdot.list;

import java.util.List;

import club.smartdot.util.RedisUtil;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.ListPosition;

public class RedisList {
	public static void main(String[] args) {
		JedisPool pool = RedisUtil.open("127.0.0.1", 6379);
		Jedis jedis = null;
		try {
			// 从连接池中获取Jedis对象
			jedis = pool.getResource();
			String key = "framework";
			jedis.del(key);
			jedis.lpush(key, "mybatis");
			jedis.lpush(key, "hibernate", "spring", "springmvc");
			List<String> lists = jedis.lrange(key, 0, -1);
			for (String str : lists) {
				System.out.println("列表数据:" + str);
			}
			System.out.println("列表长度:" + jedis.llen(key));
			System.out.println("插入新值后列表的长度:" + jedis.linsert(key, ListPosition.AFTER, "spring", "jpa"));
			// 列表右侧插入数据
			jedis.rpush(key, "struts", "webwork");
			System.out.println("列表数据:" + jedis.lrange(key, 0, -1));
			System.out.println("第一个下标的值:" + jedis.lindex(key, 1));
			
			for(long i = 0, len = jedis.llen(key); i < len; i++) {
				System.out.println("弹出值:" + jedis.lpop(key));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (jedis != null) {
				// 使用完的连接池对象，放回连接池
				jedis.close();
			}
		}
	}
}
